package cn.topcodes.tcsf.wechat.service;

import cn.topcode.unicorn.wxsdk.WXContext;
import cn.topcode.unicorn.wxsdk.material.MaterialInvoker;
import cn.topcode.unicorn.wxsdk.material.dto.UploadForeverMaterialResult;
import cn.topcodes.tcsf.admin.commons.generic.GenericService;
import cn.topcodes.tcsf.wechat.domain.entity.WechatMp;
import cn.topcodes.tcsf.wechat.domain.entity.WechatVoice;
import cn.topcodes.tcsf.wechat.service.utils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 语音素材 服务
 * @author xw
 * @date 2017-12-22 15:14:47
 */
@Service
@Transactional(readOnly = true)
public class WechatVoiceServiceImpl extends GenericService<WechatVoice> implements WechatVoiceService {

    @Autowired
    private WechatMpService wechatMpService;

    private MaterialInvoker invoker = WXContext.getMaterialInvoker();

    @Autowired
    private WechatFileService wechatFileService;

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void upload(String mpId, File file) {
        WechatVoice entity = new WechatVoice();

        entity.setTitle(file.getName());
        entity.setUrl("/voice/" + file.getName());

        long length = wechatFileService.getDuration(file);
        entity.setLength(LocalTime.ofSecondOfDay(length / 1000).format(DateTimeFormatter.ofPattern("mm:ss")));

        UploadForeverMaterialResult result = invoker.uploadForeverVoiceMaterial(mpId, file);
        if (result.getErrcode() == 0) {
            entity.setMediaId(result.getMediaId());
            WechatMp wechatMp = wechatMpService.findWechatMpByMpId(mpId);
            entity.setWechatMpId(wechatMp.getId());
            this.create(entity);
        } else {
            throw new IllegalArgumentException("errcode: " + result.getErrcode() + ", errmsg: " + result.getErrmsg());
        }
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void upload(File file) {
        this.upload(wechatMpService.getDefaultMpId(), file);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void update(WechatVoice voice) {
        WechatVoice v = this.findOne(voice.getId());
        if (v != null) {
            BeanUtils.copyProperties(v, voice);
            super.update(v);
        }
    }

    @Override
    @Transactional(readOnly = false)
    public WechatVoice delete(Long id) {
        WechatVoice voice = this.findOne(id);
        if (voice != null) {
            String mpId = wechatMpService.getMpId(voice.getWechatMpId());
            String url = voice.getUrl();
            wechatFileService.deleteFile(
                    WechatFileService.FILE_TYPE_VOICE, wechatFileService.getNameByUrl(url, "/"));
            invoker.deleteForeverMaterial(mpId, voice.getMediaId());
            return super.delete(id);
        }
        return null;
    }
}
